package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */


@RestController("UserDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品相关的接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("根据分类id查询菜品接口")
    @GetMapping("/list")
    public Result<List<DishVO>> QueryByCategoryId( Long categoryId){
        log.info("查询的菜品分类id：{}",categoryId);
        String key = "dish_" + categoryId;
        //在redis中是否有菜品缓存
        List<DishVO> cacheLines= (List<DishVO>) redisTemplate.opsForValue().get(key);
        //表示有缓存
        if(cacheLines!=null  && cacheLines.size()>0){
            return Result.success(cacheLines);
        }
        //无缓存,查数据库 并且缓冲缓存
        List<DishVO> dishVOList = dishService.getVoByCategoryId(categoryId);
        redisTemplate.opsForValue().set(key,dishVOList);
        log.info("重新载入缓存.....");
        return  Result.success(dishVOList);
    }


}
